﻿#include "widget.h"
#include "ui_widget.h"


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->stackedWidget->setCurrentIndex(0);
    ui->navbar_top->setCurrentIndex(0);
    ui->login_password_input->setEchoMode(QLineEdit::Password);
    ui->register_confirmed_password_input->setEchoMode(QLineEdit::Password);
    ui->register_password_input->setEchoMode(QLineEdit::Password);
}

Widget::~Widget()
{
    delete ui;
}

// 关闭按钮点击事件
void Widget::on_window_close_btn_clicked()
{
    this->close();
}

// 页面导航:切换页面
void Widget::switch_current_page(int i)
{
    ui->stackedWidget->setCurrentIndex(i);
}

// 侧边登录按钮点击事件
void Widget::on_window_to_user_login_btn_clicked()
{
    qDebug() << this->user->get_jwt_token() << endl;
    if (this->user->get_jwt_token().size() == 0) {
        this->switch_current_page(0);
    } else {
        QMessageBox::warning(this, "Tips:", "您已经登录过了");
    }
}

// 侧边注册按钮点击事件
void Widget::on_window_to_user_register_btn_clicked()
{
    this->switch_current_page(1);
}

// 点击确认登录按钮后的事件
void Widget::on_login_confirm_login_btn_clicked()
{
    qDebug() << "确认登录" << endl;
    QJsonObject data;
    data["username"] = ui->login_username_input->text();
    data["password"] = ui->login_password_input->text();
    data["id_kind"] = ui->login_id_kind_input->text();

//    qDebug() << "----------" << data << endl << "----------" << endl;

    QObject::connect(this->user, &User::user_login_after, this, &Widget::handle_user_login_finished);

    this->user->user_login(data);
}

// 点击确认注册按钮后的事件
void Widget::on_register_confirm_register_btn_clicked()
{
    qDebug() << "确认注册" << endl;
    QJsonObject data;
    data["username"] = ui->register_username_input->text();
    data["password"] = ui->register_password_input->text();
    data["confirmed_password"] = ui->register_confirmed_password_input->text();
    data["id_kind"] = ui->register_id_kind_input->text();
    data["invitation_code"] = ui->register_invitation_code_input->text();

//    qDebug() << "----------" << data << endl << "----------" << endl;

    QObject::connect(this->user, &User::user_register_after, this, &Widget::handle_user_register_finished);

    this->user->user_register(data);
}

// 处理登录的post请求的返回值
void Widget::handle_user_login_finished() {
    qDebug() << "登录完成后" << endl;
    if (this->user->request->get_return_code() == 200) {
        // 存储token
        this->user->set_jwt_token(this->user->request->get_return_data().toObject()["token"].toString());
        // 清空输入框
        this->empty_login_box();
        // 请求用户信息
        this->user->user_get_cur_user_info();
        QObject::connect(this->user, &User::user_get_cur_user_info_after, this, &Widget::handle_user_get_cur_user_info_finished);

        // 提示
        QMessageBox::information(this, "登录成功", "确认");

        // 跳转页面
        this->switch_current_page(2);

        this->get_cur_user_info(this->user->jwt_token, this->user->id);

    } else {
        QMessageBox::critical(this, "登录失败", this->user->request->get_return_message());
        this->empty_login_box();
    }
    QObject::disconnect(this->user, &User::user_login_after, this, &Widget::handle_user_login_finished);
}

// 处理注册的post请求的返回值
void Widget::handle_user_register_finished() {
    qDebug() << "注册完成后" << endl;
    if (this->user->request->get_return_code() == 200) {
        QMessageBox::information(this, "注册成功", "确认");
        this->empty_register_box();
        this->switch_current_page(0);
    } else {
        QMessageBox::critical(this, "注册失败", this->user->request->get_return_message());
        this->empty_register_box();
    }
    QObject::disconnect(this->user, &User::user_register_after, this, &Widget::handle_user_register_finished);
}

// 清空用于登录的输入
void Widget::empty_login_box() {
    ui->login_username_input->setText("");
    ui->login_password_input->setText("");
    ui->login_id_kind_input->setText("");
}

// 清空用于注册的输入
void Widget::empty_register_box() {
    ui->register_username_input->setText("");
    ui->register_password_input->setText("");
    ui->register_confirmed_password_input->setText("");
    ui->register_id_kind_input->setText("");
    ui->register_invitation_code_input->setText("");
}

// 处理请求用户信息获得的返回值
void Widget::handle_user_get_cur_user_info_finished() {
    ui->navbar_top_username_input->setText(this->user->username);
    ui->navbar_top_id_kind_input->setText(this->user->id_kind_string);
    QObject::disconnect(this->user, &User::user_get_cur_user_info_after, this, &Widget::handle_user_get_cur_user_info_finished);
}

// 清空上方用户信息
void Widget::empty_navbar_top() {
    ui->navbar_top_id_kind_input->setText("");
    ui->navbar_top_username_input->setText("");
}

// 点击用户信息按钮
void Widget::on_navbar_top_user_info_btn_clicked()
{
    if (this->user->user_is_login()) {
        this->switch_current_page(3);
    } else {
        QMessageBox::warning(this, "您还没有登录", "快去登录吧！");
    }

}

// 获取详细信息
void Widget::get_cur_user_info(QString token, int uid) {
    int id_kind = this->user->id_kind;
    if (id_kind==0) {
        this->client->get_client_info(token, uid);
        this->client->get_all_food();
        QObject::connect(this->client, &Client::handle_get_all_food_finished, this, &Widget::get_cur_user_info_finished);
//        this->client->get_all_order_by_client_id();
    } else if (id_kind==1) {
        this->server->get_server_info(token, uid);
    } else if (id_kind==2) {
        this->chef->get_chef_info(token, uid);
    } else if (id_kind==3) {
        this->manager->get_manager_info(token, uid);
    }
}

void Widget::get_cur_user_info_finished() {
    int id_kind = this->user->id_kind;
    if (id_kind==0) {
        for (int i = 0; i < this->client->food_list.size(); i ++ ) {
            ui->buy_food_food_table->insertRow(ui->buy_food_food_table->rowCount());
            int rowIdx = ui->buy_food_food_table->rowCount()-1;
            QJsonObject obj = this->client->food_list[i].toObject();
            //必须先设置item,然后再获取,因为默认是空的
            QTableWidgetItem *item0 = new QTableWidgetItem(QString::number(obj["id"].toInt()));
            QTableWidgetItem *item1 = new QTableWidgetItem(obj["name"].toString());
            QTableWidgetItem *item2 = new QTableWidgetItem(obj["foodStyleName"].toString());
            QTableWidgetItem *item3 = new QTableWidgetItem(QString::number(obj["price"].toDouble()));
            ui->buy_food_food_table->setItem(rowIdx,0,item0);
            ui->buy_food_food_table->setItem(rowIdx,1,item1);
            ui->buy_food_food_table->setItem(rowIdx,2,item2);
            ui->buy_food_food_table->setItem(rowIdx,3,item3);
        }
//        this->client->get_all_order_by_client_id();
    } else if (id_kind==1) {
//        this->server->get_server_info(token, uid);
    } else if (id_kind==2) {
//        this->chef->get_chef_info(token, uid);
    } else if (id_kind==3) {
//        this->manager->get_manager_info(token, uid);
    }
    QObject::disconnect(this->client, &Client::handle_get_all_food_finished, this, &Widget::get_cur_user_info_finished);
}

void Widget::handle_get_all_unpaid_order() {
    QJsonArray objs = this->server->unpaid_order_list;
//    ui->check_out_table->clear();
    int cnt = ui->check_out_table->rowCount();
    for (int i = cnt - 1; i >= 0; i -- ) {
        ui->check_out_table->removeRow(i);
    }
    for(int i = 0; i < objs.size(); i++){
         QJsonObject obj = objs[i].toObject();
         //添加新行
         ui->check_out_table->insertRow(ui->check_out_table->rowCount());
         int rowIdx = ui->check_out_table->rowCount()-1;
         //必须先设置item,然后再获取,因为默认是空的
         QTableWidgetItem *item0 = new QTableWidgetItem(QString::number(obj["id"].toInt()));
         QTableWidgetItem *item1 = new QTableWidgetItem(QString::number(obj["tableId"].toInt()));
         QTableWidgetItem *item2 = new QTableWidgetItem(obj["createTime"].toString());
         QTableWidgetItem *item3 = new QTableWidgetItem(QString::number(obj["sum"].toDouble()));
         ui->check_out_table->setItem(rowIdx,0,item0);
         ui->check_out_table->setItem(rowIdx,1,item1);
         ui->check_out_table->setItem(rowIdx,2,item2);
         ui->check_out_table->setItem(rowIdx,3,item3);
    }
}

void Widget::handle_done() {
    if (this->chef->order_item_request->get_return_code() == 200) {
        QMessageBox::information(this,"确认完成菜品成功","确认");

        this->on_navbar_top_tab_03_chef_todo_btn_clicked();
    } else {
        QMessageBox::warning(this,"确认完成菜品失败:" + this->chef->order_item_request->get_return_message(),"确认");
    }
    QObject::disconnect(this->chef, &Chef::done_signal, this, &Widget::handle_done);
}

void Widget::handle_get_all_unfinished() {
    int len = this->chef->unfinished_order_item_list.size();
    int cnt = ui->todo_food_table->rowCount();
    for (int i = cnt - 1; i >= 0; i -- ) {
        ui->todo_food_table->removeRow(i);
    }
    for (int i = 0; i < len; i ++ ) {
        QJsonObject obj = this->chef->unfinished_order_item_list.at(i).toObject();
        //添加新行
        ui->todo_food_table->insertRow(ui->todo_food_table->rowCount());
        int rowIdx = ui->todo_food_table->rowCount()-1;
        //必须先设置item,然后再获取,因为默认是空的
        QTableWidgetItem *item0 = new QTableWidgetItem(QString::number(obj["id"].toInt()));
        QTableWidgetItem *item1 = new QTableWidgetItem(obj["foodName"].toString());
        QTableWidgetItem *item2 = new QTableWidgetItem(obj["createTime"].toString());
        ui->todo_food_table->setItem(rowIdx,0,item0);
        ui->todo_food_table->setItem(rowIdx,1,item1);
        ui->todo_food_table->setItem(rowIdx,2,item2);
    }
    QObject::disconnect(this->chef, &Chef::get_all_unfinished_signal, this, &Widget::handle_get_all_unfinished);
}

// 组件隐藏相关的函数

// 组件显示相关的函数

void Widget::on_navbar_top_tabBarClicked(int index)
{
    ui->stackedWidget->setCurrentIndex(2);
}

void Widget::on_navbar_top_destroyed()
{

}

void Widget::on_navbar_top_currentChanged(int index)
{
    if (index != 0 && !this->user->user_is_login()) {
        this->ui->navbar_top->setCurrentIndex(0);
        QMessageBox::warning(this, "您还没有登录", "快去登录吧！");
    }
    else if (index != 0 && (index - 1) != this->user->id_kind) {
        this->ui->navbar_top->setCurrentIndex(0);
        QMessageBox::warning(this, "当前用户为:" + this->user->id_kind_string, "尝试切换帐号吧！");
    }
}

void Widget::on_navbar_top_tab_01_client_order_btn_clicked()
{
    this->switch_current_page(7);
}

void Widget::on_navbar_top_tab_01_client_review_btn_clicked()
{
    this->switch_current_page(9);
}

void Widget::on_navbar_top_tab_01_client_order_history_btn_clicked()
{
    this->client->get_all_order_by_client_id();
    QObject::connect(this->client, &Client::get_all_order_by_client_id_signal, this, &Widget::handle_get_all_order_by_client_id);
    this->switch_current_page(8);
}

void Widget::on_navbar_top_tab_01_client_info_btn_clicked()
{
    this->switch_current_page(3);
}

void Widget::on_navbar_top_tab_02_server_notice_btn_clicked()
{
    this->switch_current_page(10);
}

void Widget::on_navbar_top_tab_02_server_money_btn_clicked()
{
    this->server->get_all_unpaid_order();
    QObject::connect(this->server, &Server::get_all_unpaid_order_signal, this, &Widget::handle_get_all_unpaid_order);
    this->switch_current_page(11);
}

void Widget::on_navbar_top_tab_02_server_info_btn_clicked()
{
    this->switch_current_page(4);
}

void Widget::on_navbar_top_tab_03_chef_material_btn_clicked()
{
    this->switch_current_page(12);
}

void Widget::on_navbar_top_tab_03_chef_food_manage_btn_clicked()
{
    this->switch_current_page(13);
}

void Widget::on_navbar_top_tab_03_chef_todo_btn_clicked()
{
    this->chef->get_all_unfinished();
    QObject::connect(this->chef, &Chef::get_all_unfinished_signal, this, &Widget::handle_get_all_unfinished);
    this->switch_current_page(14);
}

void Widget::on_navbar_top_tab_03_chef_work_btn_clicked()
{
    this->switch_current_page(15);
}

void Widget::on_navbar_top_tab_04_manager_table_manage_btn_clicked()
{
    this->switch_current_page(16);
}

void Widget::on_navbar_top_tab_04_manager_performance_btn_clicked()
{
    this->switch_current_page(17);
}

void Widget::on_navbar_top_tab_04_manager_food_rank_btn_clicked()
{
    this->switch_current_page(18);
}

void Widget::on_navbar_top_tab_04_manager_server_rank_btn_clicked()
{
    this->switch_current_page(19);
}

void Widget::on_navbar_top_logout_btn_clicked()
{
    this->user->user_logout();
    this->empty_navbar_top();
    this->switch_current_page(0);
}

void Widget::on_navbar_top_tab_03_chef_info_btn_clicked()
{
    this->switch_current_page(5);
}

void Widget::on_add_select_food_btn_clicked()
{
    QList<QTableWidgetItem*> items = ui->buy_food_food_table->selectedItems();
//    QList<int> food_id_list;

    for (int i = 0; i < items.size(); i ++ ) {
        int fid = ui->buy_food_food_table->item(items.at(i)->row(), 0)->text().toInt();

        ui->buy_food_select_food_table->insertRow(ui->buy_food_select_food_table->rowCount());
        int rowIdx = ui->buy_food_select_food_table->rowCount()-1;

        for (int j = 0; j < this->client->food_list.size(); j ++ ) {
            QJsonObject obj = this->client->food_list[j].toObject();

            if (obj["id"].toInt() != fid) continue;
            //必须先设置item,然后再获取,因为默认是空的
            QTableWidgetItem *item0 = new QTableWidgetItem(QString::number(obj["id"].toInt()));
            QTableWidgetItem *item1 = new QTableWidgetItem(obj["name"].toString());
            QTableWidgetItem *item2 = new QTableWidgetItem(obj["foodStyleName"].toString());
            QTableWidgetItem *item3 = new QTableWidgetItem(QString::number(obj["price"].toDouble()));
            ui->buy_food_select_food_table->setItem(rowIdx,0,item0);
            ui->buy_food_select_food_table->setItem(rowIdx,1,item1);
            ui->buy_food_select_food_table->setItem(rowIdx,2,item2);
            ui->buy_food_select_food_table->setItem(rowIdx,3,item3);
        }

    }
}

void Widget::on_delete_select_food_btn_clicked()
{
    QList<QTableWidgetItem*> items = ui->buy_food_select_food_table->selectedItems();

    for (int i = 0; i < items.size(); i ++ ) {
        ui->buy_food_select_food_table->removeRow(items.at(i)->row());
    }
}

void Widget::on_buy_food_sub_order_btn_clicked()
{
    QObject::connect(this->client, &Client::send_order_finished_signal, this, &Widget::handle_send_order_finished);
    int row_num = ui->buy_food_select_food_table->rowCount();
    QJsonArray food_list;
    for (int i = 0; i < row_num; i ++ ) {
        int fid = ui->buy_food_select_food_table->item(i, 0)->text().toInt();

        for (int j = 0; j < this->client->food_list.size(); j ++ ) {
            QJsonObject obj = this->client->food_list[j].toObject();

            if (obj["id"].toInt() != fid) continue;
            obj["num"] = 1;
            food_list.append(obj);
        }
    }
    this->client->send_order(food_list);
}

void Widget::handle_send_order_finished() {
    if (this->client->order_request->get_return_code() == 200) {
        QMessageBox::information(this, "订单提交成功", "确认");
    } else {
        QMessageBox::warning(this, this->client->order_request->get_return_message(), "确认");
    }
    this->on_navbar_top_tab_01_client_order_history_btn_clicked();
    QObject::disconnect(this->client, &Client::send_order_finished_signal, this, &Widget::handle_send_order_finished);
}

void Widget::handle_get_all_order_by_client_id() {
    int len = this->client->order_list.size();
    for (int i = len - 1; i >= 0; i -- ) {
        QJsonObject obj = this->client->order_list.at(i).toObject();
        //添加新行
        ui->client_order_table->insertRow(ui->client_order_table->rowCount());
        int rowIdx = ui->client_order_table->rowCount()-1;
        //必须先设置item,然后再获取,因为默认是空的
        QTableWidgetItem *item0 = new QTableWidgetItem(QString::number(obj["id"].toInt()));
        QTableWidgetItem *item1 = new QTableWidgetItem(obj["createTime"].toString());
        QTableWidgetItem *item2 = new QTableWidgetItem(obj["finishTime"].toString());
        QTableWidgetItem *item3 = new QTableWidgetItem(QString::number(obj["tableId"].toInt()));
        QTableWidgetItem *item4 = new QTableWidgetItem(QString::number(obj["totalMoney"].toDouble()));
        ui->client_order_table->setItem(rowIdx,0,item0);
        ui->client_order_table->setItem(rowIdx,1,item1);
        ui->client_order_table->setItem(rowIdx,2,item2);
        ui->client_order_table->setItem(rowIdx,3,item3);
        ui->client_order_table->setItem(rowIdx,4,item4);
    }
    QObject::disconnect(this->client, &Client::get_all_order_by_client_id_signal, this, &Widget::handle_get_all_order_by_client_id);
}

void Widget::on_confirm_check_out_btn_clicked()
{
    QList<QTableWidgetItem*> items = ui->check_out_table->selectedItems();
    int count = items.count();

    if (count == 0) {
        QMessageBox::warning(this,"请选中对应订单","确认");
        return;
    } else if (count > 1) {
        QMessageBox::warning(this,"一次只能选中一个订单","确认");
        return;
    }

    QTableWidgetItem *item = items.at(0);
    int order_id = ui->check_out_table->item(item->row(), 0)->text().toInt();
    double total_money = ui->confirm_check_out_input->text().toDouble();
//    qDebug() << order_id << " " << ui->confirm_check_out_input->text() << endl;
    this->server->check_out(order_id, total_money);
    QObject::connect(this->server, &Server::check_out_signal, this, &Widget::handle_check_out);
}

void Widget::handle_check_out() {
    if (this->server->order_request->get_return_code() == 200) {
        QMessageBox::information(this,"确认收款成功","确认");
        this->on_navbar_top_tab_02_server_money_btn_clicked();
    } else {
        QMessageBox::warning(this,"确认收款失败:" + this->server->order_request->get_return_message(),"确认");
    }
    QObject::disconnect(this->server, &Server::check_out_signal, this, &Widget::handle_check_out);
}

void Widget::on_confirm_done_btn_clicked()
{
    QList<QTableWidgetItem*> items = ui->todo_food_table->selectedItems();
    int count = items.count();

    if (count == 0) {
        QMessageBox::warning(this,"请选中菜品","确认");
        return;
    } else if (count > 1) {
        QMessageBox::warning(this,"一次只能选中一个","确认");
        return;
    }

    QTableWidgetItem *item = items.at(0);
    int order_item_id = ui->todo_food_table->item(item->row(), 0)->text().toInt();
    this->chef->done(order_item_id);
    QObject::connect(this->chef, &Chef::done_signal, this, &Widget::handle_done);
}
